#!/bin/bash

# Slackware build script for yuzu

# Copyright 2022 Steven Voges <Oregon, USA>
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=yuzu
SRCNAM=$PRGNAM-mainline-mainline
PRGVER=${PRGVER:-"0-1144"}
VERSION=${VERSION:-0.1144}
CPPHTTPLIB=${CPPHTTPLIB:-305a7abcb9b4e9e349843c6d563212e6c1bbbf21}
CPPJWT=${CPPJWT:-e12ef06218596b52d9b5d6e1639484866a8e7067}
CUBEB=${CUBEB:-75d9d125ee655ef80f3bfcd97ae5a805931042b8}
DISCORDRPC=${DISCORDRPC:-20cc99aeffa08a4834f156b6ab49ed68618cf94a}
DYNARMIC=${DYNARMIC:-2d4602a6516c67d547000d4c80bcc5f74976abdd}
ENET=${ENET:-39a72ab1990014eb399cee9d538fd529df99c6a0}
FFMPEG=${FFMPEG:-6b6b9e593dd4d3aaf75f48d40a13ef03bdef9fdb}
GOOGLETEST=${GOOGLETEST:-800f5422ac9d9e0ad59cd860a2ef3a679588acb4}
INIH=${INIH:-1e80a47dffbda813604f0913e2ad68c7054c14e4}
LIBRESSL=${LIBRESSL:-8929f818fd748fd31a34fec7c04558399e13014a}
LIBUSB=${LIBUSB:-c6a35c56016ea2ab2f19115d2ea1e85e0edae155}
MBEDTLS=${MBEDTLS:-8c88150ca139e06aa2aae8349df8292a88148ea1}
OPUS=${OPUS:-ad8fe90db79b7d2a135e3dfd2ed6631b0c5662ab}
SANITIZERSCMAKE=${SANITIZERSCMAKE:-aab6948fa863bc1cbe5d0850bc46b9ef02ed4c1a}
SDL=${SDL:-b424665e0899769b200231ba943353a5fee1b6b6}
SIRIT=${SIRIT:-aa292d56650bc28f2b2d75973fab2e61d0136f9c}
SPIRVHEADERS=${SPIRVHEADERS:-a3fdfe81465d57efc97cfd28ac6c8190fb31a6c8}
VCPKG=${VCPKG:-9b22b40c6c61bf0da2d46346dd44a11e90972cc9}
VULKANHEADERS=${VULKANHEADERS:-33d4dd987fc8fc6475ff9ca2b4f0c3cc6e793337}
XBYAK=${XBYAK:-c306b8e5786eeeb87b8925a8af5c3bf057ff5a90}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}
DEPS=$TMP/$SRCNAM-$PRGVER/externals

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $SRCNAM-$PRGVER
tar xvf $CWD/$SRCNAM-$PRGVER.tar.gz

# yuzu has quite a few external dependencies, we need to extract them to the correct location.
tar xvf $CWD/SDL-$SDL.tar.gz -C \
  $DEPS/SDL --strip-components 1
tar xvf $CWD/Vulkan-Headers-$VULKANHEADERS.tar.gz -C \
  $DEPS/Vulkan-Headers --strip-components 1
tar xvf $CWD/cpp-httplib-$CPPHTTPLIB.tar.gz -C \
  $DEPS/cpp-httplib --strip-components 1
tar xvf $CWD/cpp-jwt-$CPPJWT.tar.gz -C \
  $DEPS/cpp-jwt --strip-components 1
tar xvf $CWD/cubeb-$CUBEB.tar.gz -C \
  $DEPS/cubeb --strip-components 1
tar xvf $CWD/sanitizers-cmake-$SANITIZERSCMAKE.tar.gz -C \
  $DEPS/cubeb/cmake/sanitizers-cmake --strip-components 1
tar xvf $CWD/googletest-$GOOGLETEST.tar.gz -C \
  $DEPS/cubeb/googletest --strip-components 1
tar xvf $CWD/discord-rpc-$DISCORDRPC.tar.gz -C \
  $DEPS/discord-rpc --strip-components 1
tar xvf $CWD/dynarmic-$DYNARMIC.tar.gz -C \
  $DEPS/dynarmic --strip-components 1
tar xvf $CWD/enet-$ENET.tar.gz -C \
  $DEPS/enet --strip-components 1
tar xvf $CWD/FFmpeg-$FFMPEG.tar.gz -C \
  $DEPS/ffmpeg/ffmpeg --strip-components 1
tar xvf $CWD/inih-$INIH.tar.gz -C \
  $DEPS/inih/inih --strip-components 1
tar xvf $CWD/ext-libressl-portable-$LIBRESSL.tar.gz -C \
  $DEPS/libressl --strip-components 1
tar xvf $CWD/libusb-$LIBUSB.tar.gz -C \
  $DEPS/libusb/libusb --strip-components 1
tar xvf $CWD/mbedtls-$MBEDTLS.tar.gz -C \
  $DEPS/mbedtls --strip-components 1
tar xvf $CWD/opus-$OPUS.tar.gz -C \
  $DEPS/opus/opus --strip-components 1
tar xvf $CWD/sirit-$SIRIT.tar.gz -C \
  $DEPS/sirit --strip-components 1
tar xvf $CWD/SPIRV-Headers-$SPIRVHEADERS.tar.gz -C \
  $DEPS/sirit/externals/SPIRV-Headers --strip-components 1
tar xvf $CWD/vcpkg-$VCPKG.tar.gz -C \
  $DEPS/vcpkg --strip-components 1
tar xvf $CWD/xbyak-$XBYAK.tar.gz -C \
  $DEPS/xbyak --strip-components 1

# yuzu checks for external components by checking for their .git files.
# Since these are not included when we downloaded the commit tarballs, we'll create them.
echo "gitdir: ../../.git/modules/SDL" > $DEPS/SDL/.git
echo "gitdir: ../../.git/modules/Vulkan-Headers" > $DEPS/Vulkan-Headers/.git
echo "gitdir: ../../.git/modules/cpp-httplib" > $DEPS/cpp-httplib/.git
echo "gitdir: ../../.git/modules/cpp-jwt" > $DEPS/cpp-jwt/.git
echo "gitdir: ../../.git/modules/cubeb" > $DEPS/cubeb/.git
echo "gitdir: ../../.git/modules/discord-rpc" > $DEPS/discord-rpc/.git
echo "gitdir: ../../.git/modules/dynarmic" > $DEPS/dynarmic/.git
echo "gitdir: ../../.git/modules/enet" > $DEPS/enet/.git
echo "gitdir: ../../../.git/modules/ffmpeg" > $DEPS/ffmpeg/ffmpeg/.git
echo "gitdir: ../../../.git/modules/inih" > $DEPS/inih/inih/.git
echo "gitdir: ../../.git/modules/libressl" > $DEPS/libressl/.git
echo "gitdir: ../../../.git/modules/libusb" > $DEPS/libusb/libusb/.git
echo "gitdir: ../../.git/modules/mbedtls" > $DEPS/mbedtls/.git
echo "gitdir: ../../../.git/modules/opus" > $DEPS/opus/opus/.git
echo "gitdir: ../../.git/modules/sirit" > $DEPS/sirit/.git
echo "gitdir: ../../.git/modules/vcpkg" > $DEPS/vcpkg/.git
echo "gitdir: ../../.git/modules/xbyak" > $DEPS/xbyak/.git

cd $PRGNAM-mainline-mainline-$PRGVER
chown -R root:root .
find -L . \
 \( -perm 777 -o -perm 775 -o -perm 750 -o -perm 711 -o -perm 555 \
  -o -perm 511 \) -exec chmod 755 {} \; -o \
 \( -perm 666 -o -perm 664 -o -perm 640 -o -perm 600 -o -perm 444 \
  -o -perm 440 -o -perm 400 \) -exec chmod 644 {} \;

mkdir -p build
cd build
  cmake \
    -Wno-dev \
    -DCMAKE_C_COMPILER=clang \
    -DCMAKE_CXX_COMPILER=clang++ \
    -DCMAKE_C_FLAGS:STRING="$SLKCFLAGS" \
    -DCMAKE_CXX_FLAGS:STRING="$SLKCFLAGS" \
    -DCMAKE_ENABLE_WEB_SERVICE=OFF \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DCMAKE_BUILD_TYPE=Release ..
  make
  make install/strip DESTDIR=$PKG
cd ..

rm -f $PKG/{,usr/}lib${LIBDIRSUFFIX}/*.la

find $PKG -print0 | xargs -0 file | grep -e "executable" -e "shared object" | grep ELF \
  | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null || true

find $PKG -name perllocal.pod -o -name ".packlist" -o -name "*.bs" | xargs rm -f || true

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a \
  *.md LICENSE.txt \
  $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
